﻿/*
 * Copyright 2007 (c) Andre Stubbe, http://www.andrestubbe.com
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.Plane;
	import org.papervision3d.scenes.MovieScene3D;

	[SWF(width='400',height='400',backgroundColor='0x000000',frameRate='40')]
	
	public class BitmapVertices extends Sprite
	{
		private var container :Sprite;
		private var scene :MovieScene3D;
		private var camera :Camera3D;
		private var plane :Plane;

		public function BitmapVertices()
		{
			container = new Sprite;
			container.x = 200;
			container.y = 200;
			addChild( container );
			
			scene = new MovieScene3D( container );
			
			camera = new Camera3D();
			camera.z = -50;
			camera.zoom = 2;

			// create a plane
			plane = new Plane( new BitmapFileMaterial("texture.jpg"), 128, 128, 8, 8);
			plane.material.smooth = true;
			plane.material.doubleSided = true;
			plane.material.lineAlpha = 0.2;
			plane.rotationX = -45;

			// register the plane
			scene.addChild( plane );
			
			// create an enterframe loop
			stage.addEventListener( Event.ENTER_FRAME, onEnterFrame );
			
		}
		
		private function onEnterFrame( event: Event ): void
		{
			plane.rotationY = mouseX;
			plane.rotationX = mouseY;
			// change the Z coordinate of every vertex
			for(var i:uint = 0;i<plane.geometry.vertices.length; i++){
				plane.geometry.vertices[i].z = Math.random()*10;
			}
			
			// render the scene
			scene.renderCamera( camera );

		}

	}
	
}

